//
//  UIImage+AG.swift
//  ASW
//
//  Created by zhuxuhong on 2020/12/2.
//

import UIKit

extension UIImage {
    public func bw_compressedData(withMaxSize bytes: UInt64) -> Data? {
        var quality: CGFloat = 1
        guard var data = jpegData(compressionQuality: 1) else {
            return nil
        }
        if data.count <= bytes {
            return data
        }
        while data.count > bytes {
            quality -= 0.1
            guard let compressed = jpegData(compressionQuality: quality) else {
                break
            }
            data = compressed
        }
        return data
    }
}

extension UIImage {
    public static func bw_transparentImage() -> UIImage? {
        return UIColor.clear.bw_toImage()
    }
}
